var angular = require("angular");

angular
    .module("work_page_main", [
        "work_page_route",
        "aboutModule",
        "homeModule",
        "newWorkModule",
        "allWorkModule",
        "utilsModule",
        "userModule"
    ])
    .constant("urlPrefix", "http://192.168.1.5:9097")
    .service("SAN_GLOBAL", [
        function(){
            var
                serv = this
            ;

            init();

            serv.init = init;
            serv.setKeyInLocalStorage = setKeyInLocalStorage;
            serv.destroy = destroy;

            function init(){
                serv.userBaseInfo = null || localStorage.getItem("userBaseInfo") && JSON.parse(localStorage.getItem("userBaseInfo"));
                serv.userLoginInfo = null || localStorage.getItem("userLoginInfo") && JSON.parse(localStorage.getItem("userLoginInfo"));
            }

            function setKeyInLocalStorage(key, value){
                localStorage.setItem(key, JSON.stringify(value));
                serv[key] = value;
            }

            function destroy(){
                serv.userBaseInfo = null;
                serv.userLoginInfo = null;
                localStorage.clear();
            }
        }
    ])

.constant("HOME_NAV_LIST", [{
            name: "首页",
            route: "home",
            id: 1,
            selected: true,
            controller: "homeCtrl"
        },
        {
            name: "全部兼职",
            route: "all_job",
            navId: 2,
            selected: false,
            controller: "allJobCtrl"
        },
        {
            name: "最新兼职",
            route: "new_job",
            navId: 3,
            selected: false,
            controller: "newJobCtrl"
        },
        {
            name: "关于我们",
            route: "about",
            navId: 4,
            selected: false,
            controller: "aboutCtrl"
        }
    ])
    .controller("workMainCtrl", [
        "$location",
        "HOME_NAV_LIST",
        function($location, HOME_NAV_LIST) {
            var
                _vm_ = this;

            init();

            _vm_.switchUrl = switchUrl;

            function init() {
                _vm_.test = "hello world";
                _vm_.navList = HOME_NAV_LIST;
                _initRoute(_vm_.navList[0]);
            }

            function switchUrl(navItem) {
                _initRoute(navItem);
            }

            function _initRoute(navItem) {
                _vm_.navList.map(function(item) {
                    if (item.navId === navItem.navId) {
                        item.selected = true;
                    } else {
                        item.selected = false;
                    }
                });
            }
        }
    ]);